package com.ynny.controller;

import com.ynny.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class DataBindTest01Controller {


    //1.原生servlet 绑定数据

    @GetMapping("/test01")
    @ResponseBody
    public String test01(HttpServletRequest request){

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");

        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);

        return "test01";
    }



    //2.SpringMVC绑定数据
    @GetMapping("/test02")
    @ResponseBody
    public String test02(@RequestParam(name = "id") Integer id,
                         @RequestParam(name = "name") String name,
                         @RequestParam(name = "age",required = false) Integer age){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return "test02";
    }

    //3.SpringMVC占位符方式  Restful风格
    @GetMapping("/test03/{id}/{name}/{age}")
    @ResponseBody
    public String test03(@PathVariable(name = "id") Integer id,
                         @PathVariable(name = "name") String name,
                         @PathVariable(name = "age") Integer age){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        return "test03";
    }


    //4.SpringMVC实体类对象绑定数据
    @GetMapping("/test04")
    @ResponseBody
    public String test04(User user){
        System.out.println("user = " + user);
        return "test04";
    }


    //5.SpringMVC Map集合对象绑定数据

    @GetMapping("/test05")
    @ResponseBody
    public String test05(@RequestParam Map<String,Object> map){
        System.out.println("map = " + map);
        return "test05";
    }

}
